package com.sls.game.parser.ohfei;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sls.game.parser.nutsnet.EncodeChineseCode;
import com.sls.game.pojo.OhFeiResponseInfo;

/**
 * 
 * @description 以SAX方式解析xml字符串，余额查询解析
 * @author Wangxuegang
 * @date 2012-9-27
 * 
 */
public class OhFeiResOfBalanceSaxParseXml extends DefaultHandler {

	private List<OhFeiResponseInfo> list = null;

	private OhFeiResponseInfo ohFeiResponseInfo = null;

	// 用来存放每次遍历后的元素名称(节点名称)
	private String tagName;

	private static Logger log = Logger
			.getLogger(OhFeiResOfBalanceSaxParseXml.class);

	/**
	 * 对返回的xml格式字符串数据进行解析
	 * 
	 * @param resultStr
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 * @throws SAXException
	 * @throws ParserConfigurationException
	 * @throws DocumentException
	 */
	public static List<OhFeiResponseInfo> parseXmlString(String xmlStr)
			throws UnsupportedEncodingException {
		String encodeXmlStr = EncodeChineseCode
				.encodeChineseCodeOfGB2312(xmlStr);
		// 加载资源文件 转化为一个输入流
		byte[] xmlStrByte = encodeXmlStr.getBytes();
		ByteArrayInputStream in = new ByteArrayInputStream(xmlStrByte);
		try {
			// 构建SAXParser
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();

			OhFeiResOfBalanceSaxParseXml handler = new OhFeiResOfBalanceSaxParseXml();
			parser.parse(in, handler);
			return handler.getList();
		} catch (ParserConfigurationException ep) {
			log.error(ep);
		} catch (SAXException es) {
			log.error(es);
		} catch (IOException e) {
			log.error(e);
		}

		return null;
	}

	// 只调用一次 初始化list集合
	@Override
	public void startDocument() throws SAXException {
		list = new ArrayList<OhFeiResponseInfo>();
	}

	// 调用多次 开始解析
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (qName.equals("userinfo")) {
			ohFeiResponseInfo = new OhFeiResponseInfo();
		}
		this.tagName = qName;
	}

	// 调用多次
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (qName.equals("userinfo")) {
			this.list.add(this.ohFeiResponseInfo);
		}
		this.tagName = null;
	}

	// 只调用一次
	@Override
	public void endDocument() throws SAXException {
	}

	// 调用多次
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (this.tagName != null) {
			String data = new String(ch, start, length);

			if (this.tagName.equals("err_msg")) {
				this.ohFeiResponseInfo.setErr_msg(data);
			} else if (this.tagName.equals("retcode")) {
				this.ohFeiResponseInfo.setRetcode(data);
			} else if (this.tagName.equals("ret_leftcredit")) {
				this.ohFeiResponseInfo.setRet_leftcredit(data);
			}
		}
	}

	public List<OhFeiResponseInfo> getList() {
		return list;
	}

	public void setList(List<OhFeiResponseInfo> list) {
		this.list = list;
	}

	public OhFeiResponseInfo getOhFeiResponseInfo() {
		return ohFeiResponseInfo;
	}

	public void setOhFeiResponseInfo(OhFeiResponseInfo ohFeiResponseInfo) {
		this.ohFeiResponseInfo = ohFeiResponseInfo;
	}

	public String getTagName() {
		return tagName;
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

}
